package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.mapper.*;
import com.atguigu.gmall.product.model.*;
import com.atguigu.gmall.product.service.SpuManageService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class SpuManageServiceImpl implements SpuManageService {

    @Autowired
    private SpuInfoMapper spuInfoMapper;
    @Autowired
    private SpuImageMapper spuImageMapper;

    @Autowired
    private SpuPosterMapper spuPosterMapper;
    @Autowired
    private SpuSaleAttrMapper spuSaleAttrMapper;
    @Autowired
    private SpuSaleAttrValueMapper spuSaleAttrValueMapper;

    /**
     * spu分页列表根据id查找
     *
     * @param spuInfoPage
     * @param category3Id
     * @return
     */
    @Override
    public Page<SpuInfo> getPage(Page<SpuInfo> spuInfoPage, Long category3Id) {
        LambdaQueryWrapper<SpuInfo> spuInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        spuInfoLambdaQueryWrapper.eq(SpuInfo::getCategory3Id, category3Id);
        Page<SpuInfo> spuInfoPage1 = spuInfoMapper.selectPage(spuInfoPage, spuInfoLambdaQueryWrapper);

        return spuInfoPage1;
    }

    /**
     * 保存spu
     *
     * @param spuInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSpuInfo(SpuInfo spuInfo) {
        //1.将商品的信息保存到spuinfo对象中
        spuInfoMapper.insert(spuInfo);
        Long spuId = spuInfo.getId();
        //2.将图片信息封装在spuimage集合中
        List<SpuImage> spuImageList = spuInfo.getSpuImageList();
        if (!CollectionUtils.isEmpty(spuImageList)) {
            spuImageList.stream().forEach((spuImage) -> {
                spuImage.setSpuId(spuId);
                spuImageMapper.insert(spuImage);
            });
        }
        //3.将海报保存到spupost集合中
        List<SpuPoster> spuPosterList = spuInfo.getSpuPosterList();
        if (!CollectionUtils.isEmpty(spuPosterList)) {
            spuPosterList.stream().forEach((spuPoster) -> {
                spuPoster.setSpuId(spuId);
                spuPosterMapper.insert(spuPoster);
            });
        }
        //4.将销售属性名称sousaleattr 存入spusaleattr表
        List<SpuSaleAttr> spuSaleAttrList = spuInfo.getSpuSaleAttrList();
        if (!CollectionUtils.isEmpty(spuSaleAttrList)) {
            spuSaleAttrList.stream().forEach((spuSaleAttr -> {
                spuSaleAttr.setSpuId(spuId);
                spuSaleAttrMapper.insert(spuSaleAttr);
                //将销售属性值进行存储
                List<SpuSaleAttrValue> spuSaleAttrValueList = spuSaleAttr.getSpuSaleAttrValueList();
                //判断
                if (!CollectionUtils.isEmpty(spuSaleAttrValueList)) {
                    spuSaleAttrValueList.stream().forEach(spuSaleAttrValue -> {
                        spuSaleAttrValue.setSaleAttrName(spuSaleAttr.getSaleAttrName());
                        spuSaleAttrValue.setSpuId(spuId);
                        spuSaleAttrValueMapper.insert(spuSaleAttrValue);
                    });
                }
            }));
        }
    }

    /**
     * 根据spuId 查询销售属性
     *
     * @param spuId
     * @return
     */
    @Override
    public List<SpuSaleAttr> getSpuSaleAttrList(Long spuId) {

        List<SpuSaleAttr> spuSaleAttrList = spuSaleAttrMapper.getSpuSaleAttrList(spuId);


        return spuSaleAttrList;
    }

    /**
     * 根据spuId 获取spuImage 集合
     *
     * @param spuId
     * @return
     */
    @Override
    public List<SpuImage> getSpuImageList(Long spuId) {
        LambdaQueryWrapper<SpuImage> spuImageLambdaQueryWrapper = new LambdaQueryWrapper<>();
        spuImageLambdaQueryWrapper.eq(SpuImage::getSpuId, spuId);
        return spuImageMapper.selectList(spuImageLambdaQueryWrapper);

    }
    /**
     * 根据id查找spuinfo属性
     * @param id
     * @return
     */
    @Override
    public SpuInfo getSpuInfo(Long id) {
        SpuInfo spuInfo = spuInfoMapper.selectById(id);
        return spuInfo;
    }

    /**
     * 根据spuId 获取海报数据
     * @param spuId
     * @return
     */
    @Override
    public List<SpuPoster> findSpuPosterBySpuId(Long spuId) {
        LambdaQueryWrapper<SpuPoster> spuPosterLambdaQueryWrapper = new LambdaQueryWrapper<>();
        spuPosterLambdaQueryWrapper.eq(SpuPoster::getSpuId,spuId);
        return spuPosterMapper.selectList(spuPosterLambdaQueryWrapper);
    }

    /**
     * 根据spuId,skuId 获取销售属性数据
     * @param skuId
     * @param spuId
     * @return
     */
    @Override
    public List<SpuSaleAttr> getSpuSaleAttrListCheckBySku(Long skuId, Long spuId) {
        return   spuSaleAttrMapper.getSpuSaleAttrListCheckBySku(skuId,spuId);

    }

}
